<template>
    <Modal
            width="800"
            v-model="visible"
            :title="title"
            :key="key"
    >
        <Spin size="large" fix v-if="Loading"></Spin>
        <Form ref="model" :model="model" :rules="ruleValidate" inline :label-width="200" :disabled=disableSubmit>
            <Row>
                <FormItem label="核减类型:" prop="ssca0109">
                    <c-dict-select v-model="model.ssca0109" dictCode="subtract_type" :multiple="true" :width="400"
                                   :hiddenKeys="hiddenKeys"/>
                </FormItem>
            </Row>
            <Row>
                <FormItem label="非义务教育阶段学生人数:" prop="ssca0111" :rules="ruleValidate.ssca0111">
                    <InputNumber :min="1" v-model="model.ssca0111" style="width: 400px"/>
                </FormItem>
            </Row>
            <Row>
                <FormItem label="收入低于低保标准的60周岁以上老年人、非重度残疾人、优抚对象人数:" prop="ssca0112" :rules="ruleValidate.ssca0112">
                    <InputNumber :min="1" v-model="model.ssca0112" style="width: 400px"/>
                </FormItem>
            </Row>
            <Row>
                <FormItem label="核减说明:" prop="ssca0114">
                    <c-text-area v-model="model.ssca0114" width="400"/>
                </FormItem>
            </Row>
        </Form>
        <div slot="footer">
            <Button @click="cancel">取消</Button>
            <template v-if="!disableSubmit">
                <Button type="primary" @click="ok">确定</Button>
            </template>
        </div>
    </Modal>
</template>

<script>

    import {getAction, postAction} from '@/api/request';

    import {CommonMixin} from '@/views/common/js/CommonMixin'


    export default {
        components: {},
        name: "ss11011",
        mixins: [CommonMixin],
        data() {
            const validatessca0111 = (rule, value, callback) => {
                if (!this.model.ssca0111 && rule.required) {
                    callback(new Error('请输入正确的人数'));
                } else {
                    callback();
                }
            };
            const validatessca0112 = (rule, value, callback) => {
                if (!this.model.ssca0112 && rule.required) {
                    callback(new Error('请输入正确的人数'));
                } else {
                    callback();
                }
            };
            return {
                key: "",
                title: "",
                Loading: false,
                model: {},
                ruleValidate: {
                    ssca0109: [],
                    ssca0111: [],
                    ssca0112: [],
                    ssca0114: [],
                },
                fieldsToValidate: ['ssca0109', 'ssca0114'],
                rule: {
                    ssca0109: [
                        {required: true, message: '请选择核减类型', trigger: 'change'}
                    ],
                    ssca0111: [
                        {
                            required: true,
                            message: '请输入正确的人数',
                            trigger: 'blur',
                            type: 'number',
                            validator: validatessca0111
                        },
                    ],
                    ssca0112: [
                        {
                            required: true,
                            message: '请输入正确的人数',
                            trigger: 'blur',
                            type: 'number',
                            validator: validatessca0112
                        },
                    ],
                    ssca0114: [
                        {required: true, message: '请输入核减说明', trigger: 'blur'}
                    ],
                },
                visible: false,
                disableSubmit: false,
                hiddenKeys: '',
                url: {
                    update: '/api/ac/subsistence/ss12001Service/updateSubtract',
                },
            }
        },

        watch: {
            'model.ssca0109': function (val) {
                this.fieldsToValidateRemove(['ssca0111', 'ssca0112']);
                if (val) {
                    if (val.indexOf("03") > -1) {
                        this.fieldsToValidateAdd(['ssca0111']);
                    }
                    if (val.indexOf("04") > -1) {
                        this.fieldsToValidateAdd(['ssca0112']);
                    }
                }
                this.setRequired(this.fieldsToValidate);
            },
        },
        methods: {
            cancel() {
                this.visible = false;
            },
            ok() {
                console.log(this.ruleValidate);
                var that = this;
                this.$refs['model'].validate((valid) => {
                    if (valid) {
                        that.submitFrom();
                    }
                })
            },
            submitFrom() {
                var that = this;
                that.Loading = true;
                that.model.id = this.$parent.familyInfo.ss12001Id;
                postAction(this.url.update, that.model).then((res) => {
                    if (res.data.errcode != 0) {
                        this.$Message.error(res.data.errmsg);
                    } else {
                        that.$Message.success("保存完成！");
                        this.$emit('ok');
                        this.visible = false;
                    }
                }).finally(() => {
                    that.Loading = false;
                });
            },
            add() {
                this.edit({});
            },
            edit(row) {
                this.model = Object.assign({}, row);
                if (!this.model.ssca0111) {
                    this.model.ssca0111 = null;
                }
                if (!this.model.ssca0112) {
                    this.model.ssca0112 = null;
                }
                this.setRequired(this.fieldsToValidate);
                if (this.$parent.familyInfo.ssca0002 == '101' || this.$parent.familyInfo.ssca0002 == '201') {
                    this.hiddenKeys = '';
                } else {
                    this.hiddenKeys = '02';
                }
                this.visible = true;
            },
            //设置必填信息
            setRequired(fieldsToValidate) {
                var ruleValidate = this.ruleValidate;
                for (let key in ruleValidate) {
                    this.$set(this.ruleValidate, key, [{required: false}]);
                }
                var rule = this.rule;
                for (var i in fieldsToValidate) {
                    this.$set(this.ruleValidate, fieldsToValidate[i], rule[fieldsToValidate[i]][0])
                }
                console.log(this.ruleValidate)
            },
        }
    }
</script>

<style scoped>
</style>